<?php

namespace App\Http\Controllers\Admin;

use App\DTOS\Admin\AdminDTO;
use App\Http\Controllers\Controller;
use App\Models\Admin;
use App\Models\Customer;
use App\Models\Game\ServerGroup;
use App\Services\AdminService;
use App\Services\AuthorityService;
use App\Utils\MyLog;
use App\Utils\ResponseUtil;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Str;


// 管理员控制器
class AdminController extends Controller
{
    private Request $request;
    private AdminService $adminService;
    private AuthorityService $authorityService;

    /**
     * AdminController constructor.
     * @param Request $request
     * @param AdminService $adminService
     */
    public function __construct(Request $request, AdminService $adminService,AuthorityService $authorityService)
    {
        $this->request = $request;
        $this->adminService = $adminService;
        $this->authorityService = $authorityService;
    }

    public function add()
    {
        $data = $this->request->only('username','password');
        $data['created_at'] = now();
        $data['password'] = bcrypt($data['password']);
        Admin::query()->insert($data);
        return ResponseUtil::success();
    }

    public function paginate()
    {
        $dto = new AdminDTO($this->request->only('username','page','limit'));
        $paginate = $this->adminService->paginate($dto);
        $res = [];
        $res['code'] = 0;
        $res['data'] = $paginate->items();
        return ResponseUtil::json($res);
    }
    public function detail($id)
    {
        $detail = Admin::query()->with('roles')->find($id);
        $groups_ids = $detail['groups_ids'];
        $groups_ids_array = explode(",", $groups_ids);
        $groups = ServerGroup::whereIn('id',$groups_ids_array)->get();
        $detail->groups = $groups;

        return ResponseUtil::success($detail);
    }

    public function edit($id)
    {
        $data = $this->request->only('username');
        Admin::query()->where('id',$id)->update($data);
        return ResponseUtil::success();
    }

    public function resetPass($id) //重置密码
    {
        $password = $this->request->post('password');
        $admin = Admin::query()->find($id);
        $admin->password = bcrypt($password);
        $admin->save();
        return ResponseUtil::success();
    }

    public function resetSelfPass(){ //修改自己的密码
        $post = $this->request->only(['old_password','new_password']);
        $admin = Auth::guard('admin')->user();
        $admin = Admin::query()->find($admin->getAuthIdentifier());
        $oldPass = data_get($post,'old_password');
        $newPass = data_get($post,'new_password');

        $bool = Auth::guard('admin')->attempt([
            'username'=>$admin->username,
            'password'=>$oldPass
        ]);

         if (!$bool){
             return ResponseUtil::error("旧密码错误");
         }
        $admin->password = bcrypt($newPass);
        $admin->save();

        return ResponseUtil::success();

    }

    public function delete($id)
    {
        if ($id == 1 ){
            return ResponseUtil::error("不允许删除超级管理员");
        }
        Admin::query()->find($id)->delete();
        return ResponseUtil::success();
    }

    public function auth($id) //授权
    {
        $admin = Admin::query()->find($id);
        $data = $this->request->only('role_ids','groups_ids');
        $roleIds = array_values(data_get($data,'role_ids',[]));

        $groupsids = array_values(data_get($data,'groups_ids',[]));
        $groupsIdsString = implode(',', $groupsids);
        // 将拼接后的字符串存储到相应的字段
        MyLog::info('服分组'.json_encode($groupsIdsString));
        $admin->groups_ids=$groupsIdsString;
        $admin->save();

        $admin->syncRoles( $roleIds ); //授予用户角色 , 所有当前角色将被从用户中删除，而由传入的数组取代
        $this->authorityService->refreshPermission();
        return ResponseUtil::success();
    }



}
